//
//  PlayMusicTools.m
//  时光
//
//  Created by dllo on 16/4/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PlayMusicTools.h"


@interface PlayMusicTools ()


@property (nonatomic, retain) NSTimer *time;


@end


@implementation PlayMusicTools

+(PlayMusicTools *)shareMusic{
    static  PlayMusicTools *play =nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        play = [[PlayMusicTools alloc]init];
    });
    
    return play;
}

//初始化并重写
-(instancetype)init
{
    self = [super  init];
    if (self) {
        
        _player = [[AVPlayer alloc]init];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playNextMusic) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
        
        
    }
    return self;
}

-(void)playNextMusic {

    if ([self.delegate respondsToSelector:@selector(playNextMusic)]){

        [self.delegate playNextMusic];
    }

    
}


//播放
-(void)play
{
    [self.player play];
    
    
    if (self.time == nil) {
        
        //自动开启
        self.time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(star) userInfo:nil repeats:YES];
    }
    else {
        return;
    }
    
}
//暂停
-(void)pause {
    
    [self.player pause];
    
    [self stop];
}

//准备播放
-(void)preparePlayMusicWithUrl:(NSString *)url
{
    
    AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:url]];
    [self.player replaceCurrentItemWithPlayerItem:item];
    
    [self play];
    
    
}

//是否在播放
-(BOOL)isPlaying
{
    return self.player.rate;
    
}


//获取当前时间
-(NSInteger)currentTime
{
    
    CMTime time = self.player.currentTime;
    
    if (time.timescale == 0) {
        return 0;
    }
    
    return time.value/time.timescale;
    
}

//总时间
-(NSInteger)totalTime
{
    CMTime time = self.player.currentItem.duration;
    if (time.timescale == 0) {
        return 0;
    }
    
    
    return time.value/time.timescale;
}

//把时间转化为字符串
-(NSString *)changeSecondToTime:(NSInteger)second {
    
    NSInteger min = second /60;
    NSInteger sed = second %60;
    
    return [NSString stringWithFormat:@"%02ld:%02ld",min,sed];
    
    
}


//定时器方法
-(void)star
{
    if ([self.delegate respondsToSelector:@selector(playMusicCurrenTime:totalTime:Progress:)]) {
    
        NSString * curren = [self changeSecondToTime:[self currentTime]];
        NSString *total = [self changeSecondToTime:[self totalTime]];
        CGFloat progress = [self fechTotalTime];
//    NSLog(@"=======%@", curren);
        [self.delegate playMusicCurrenTime:curren totalTime:total Progress:progress];
    
    }
    
}

//关闭定时器
-(void)stop
{
    [self.time invalidate ];
    self.time = nil;
    
}

//滑块
-(CGFloat)fechTotalTime
{
    CGFloat current = (CGFloat)[self currentTime];
    CGFloat total = (CGFloat)[self totalTime];
    
    return current/total;
}


@end
